package cn.lopr.im.android.mvp.presenter;

import java.util.List;

import cn.lopr.im.android.bean.CallbackResult;
import cn.lopr.im.android.bean.User;
import cn.lopr.im.android.common.OnCallBackListener;
import cn.lopr.im.android.common.app.Fragment;
import cn.lopr.im.android.db.entity.Message;
import cn.lopr.im.android.mvp.contact.IHomeContact;
import cn.lopr.im.android.mvp.model.ContactModel;
import cn.lopr.im.android.mvp.model.HomeModel;
import cn.lopr.im.android.utils.UiUtils;

/**
 * @author shuangyang7
 * @date 2021/1/3 15:48
 */
public class HomePresenterImpl implements IHomeContact.IHomePresenter {

    private final ContactModel contactModel;

    private final IHomeContact.IHomeModel homeModel;
    private final IHomeContact.IHomeView homeView;
    private final Fragment fragment;

    public HomePresenterImpl(Fragment fragment, IHomeContact.IHomeView homeView) {
        this.contactModel = new ContactModel();
        this.homeModel = new HomeModel();
        this.homeView = homeView;
        this.fragment = fragment;
    }

    @Override
    public void getFriend() {

        if (!UiUtils.isNetworkAvailable()) {
            homeView.showNetworkError();
            return;
        }

        contactModel.getFriend(this.homeView.getToken(this.fragment.getContext()), new OnCallBackListener<CallbackResult<List<User>>, String>() {
            @Override
            public void success(CallbackResult<List<User>> result) {
                homeView.getFriendSuccess(result.getData());
            }

            @Override
            public void failure(String errMsg) {
                homeView.showVerifyFailed(errMsg);
            }
        });
    }

    @Override
    public void getOffline() {
        if (!UiUtils.isNetworkAvailable()) {
            homeView.showNetworkError();
            return;
        }

        homeModel.getOffline(this.homeView.getToken(this.fragment.getContext()), new OnCallBackListener<CallbackResult<List<Message>>, String>() {
            @Override
            public void success(CallbackResult<List<Message>> data) {
                List<Message> messageResult = data.getData();
                homeView.getOfflineSuccess(messageResult);
            }

            @Override
            public void failure(String errMsg) {
                homeView.showVerifyFailed(errMsg);
            }
        });
    }
}
